package com.cat.dataStructure02;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/number-of-flowers-in-full-bloom/
 * @create 2025/10/23 17:13
 * @since JDK17
 */

public class Solution54 {
    public int[] fullBloomFlowers(int[][] flowers, int[] people) {
        int n = flowers.length, m = people.length;
        int[] starts = new int[n], ends = new int[n];
        for (int i = 0; i < n; i++) {
            starts[i] = flowers[i][0];
            ends[i] = flowers[i][1];
        }
        Arrays.sort(starts);
        Arrays.sort(ends);
        // 开花数 - 凋落数
        for (int i = 0; i < m; i++) {
            people[i] = find(starts, people[i] + 1) - find(ends, people[i]);
        }

        return people;
    }

    int find(int[] nums, int target) {
        int l = -1, r = nums.length, mid;
        while (l + 1 < r) {
            mid = l + r >> 1;
            if (nums[mid] >= target) {
                r = mid;
            } else {
                l = mid;
            }
        }

        return r;
    }
}
